// $codepro.audit.disable numericLiterals, com.instantiations.assist.eclipse.analysis.unusedReturnValue
/**
 * Class: LOGIN FRAME
 * Description:
 * 
 */
import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JPasswordField;


import javax.swing.SwingConstants;
import java.awt.Font;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;





/**
 * @author Team Awesome Express
 * @version $ Revision 1.0 $
 */
public class LoginFrame extends JFrame {

	/**
	 * Field contentPane.
	 */
	private final JPanel contentPane;
	
	/**
	 * Field panel.
	 */
	private final JPanel panel = new JPanel();
	
	/**
	 * Field txtUsername.
	 */
	private final JTextField txtUsername;
	
	/**
	 * Field btnLogin.
	 */
	private final JButton btnLogin;
	
	/**
	 * Field btnSignUp.
	 */
	private final JButton btnSignUp;
	
	/**
	 * Field myDataManager.
	 */
	private DataManager myDataManager;
	
	/**
	 * Field signUpDataManager.
	 */
	private final DataManager signUpDataManager = new DataManager(); // For signing up patients
	
	/**
	 * Field lblPassword.
	 */
	private final JLabel lblPassword;
	
	/**
	 * Field lblUsername.
	 */
	private final JLabel lblUsername;
	
	/**
	 * Field acct.
	 */
	private CreateAccount acct;

	/**
	 * Launch the application.
	 */
	public static LoginFrame Lframe = new LoginFrame();

	/**
	 * Field welcomeLabel.
	 */
	private final JLabel welcomeLabel;

	/**
	 * Field passwordField.
	 */
	private final JPasswordField passwordField;

	/**
	 * Method main.
	 * @param args String[]
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Lframe.setVisible(true);
	
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}

		});
	}

	/**
	 * Create the frame.
	 */
	private LoginFrame() {
		setTitle("Login Screen");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 503, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane); 
		panel.setBackground(GlobalVars.BACKGROUND);
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		txtUsername = new JTextField();
		txtUsername.setBounds(176, 138, 140, 20);
		panel.add(txtUsername);
		txtUsername.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setColumns(10);
		passwordField.setBounds(176, 185, 140, 20);
		
		passwordField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
					performLogin();
				}
			}
		});
		
		panel.add(passwordField);
		
		btnLogin = new JButton("Login");
		
		btnLogin.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				performLogin();
			}
			
		});
		
		btnLogin.setBounds(186, 216, 106, 23);
		panel.add(btnLogin);
		
		btnSignUp = new JButton("Sign Up");
		btnSignUp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0){
				acct = new CreateAccount(signUpDataManager);
			}
		});
		btnSignUp.setBounds(186, 252, 106, 23);
		panel.add(btnSignUp);
		
		lblPassword = new JLabel("Password:");
		lblPassword.setBounds(108, 187, 81, 17);
		panel.add(lblPassword);
		
		lblUsername = new JLabel("Username:");
		lblUsername.setBounds(108, 141, 81, 14);
		panel.add(lblUsername);
		
		final JLabel lblPleaseEnterYour = new JLabel(
					"Please enter your username and password:"
					);
		lblPleaseEnterYour.setBounds(108, 83, 290, 28);
		panel.add(lblPleaseEnterYour);
		
		welcomeLabel = new JLabel("Welcome to St. Burdell's");
		welcomeLabel.setFont(new Font("Lucida Grande", Font.BOLD, 20));
		welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		welcomeLabel.setBounds(45, 30, 388, 41);
		panel.add(welcomeLabel);
		
		final JLabel buzzLabel = new JLabel("Buzz");
		buzzLabel.setIcon(new ImageIcon("./src/buzz2.jpg"));
		buzzLabel.setBounds(-1, -8, 60, 55);
		panel.add(buzzLabel);
		
		final JLabel gtLabel = new JLabel("GT");
		gtLabel.setIcon(new ImageIcon("./src/GTlogo.gif"));
		gtLabel.setBounds(-1, 262, 53, 42);
		panel.add(gtLabel);
		
		final JLabel gtLabel2 = new JLabel("GT");
		gtLabel2.setIcon(new ImageIcon("./src/GTlogo.gif"));
		gtLabel2.setBounds(424, -1, 53, 41);
		panel.add(gtLabel2);
		
		final JLabel buzzLabel2 = new JLabel("Buzz");
		buzzLabel2.setIcon(new ImageIcon("./src/buzz2.jpg"));
		buzzLabel2.setBounds(424, 259, 53, 42);
		panel.add(buzzLabel2);
		
		
		
		
	}
	
	/**
	 * Method that will perform the login 
	 * Checks the username and password
	 * then creates a datamanager object calling
	 *  the login method with the username and password
	 */
	public synchronized void performLogin() {
		 final String username = txtUsername.getText();
         final char[] passwordArray = passwordField.getPassword();
         final String password = new String(passwordArray);
         
         if (username.equals("")) {
        	 JOptionPane.showMessageDialog(this, "You cannot have an empty username field");
        	 return;
         }
         else if (password.equals("")) {
        	 JOptionPane.showMessageDialog(this, "You cannot have an empty password field");
        	 return;
         }
         
         myDataManager = DataManager.getDataManager(username, password); // Check Login and start the dataManager spreadsheet 

         final int userType  = myDataManager.login(username, password); // { // Should be used to check the Administrative Abilities according to login
         GlobalVars.initializeGlobalVars();
         myDataManager.setUserType(userType); // Set the User type
         
         if (userType < 0){ // Failed log in 
        	 JOptionPane.showMessageDialog(this, "Login Failed");
        	 return;
         }
         final String name = myDataManager.userInfo(username, userType); //get name and last name
         
         //Clear username and password so next person to log in can't see other username/password
         txtUsername.setText("");
         passwordField.setText("");
         
         switch (userType){
         case GlobalVars.PATIENT_USERTYPE:
        	 PatientPanel.createWindow(name, username, myDataManager);
        	 Lframe.dispose();
        	 break;
         case GlobalVars.NURSE_USERTYPE:
        	 NursePanel.createWindow(name, myDataManager); //Creates the NursePanel
        	 Lframe.dispose();
        	 break;
         case GlobalVars.DOCTOR_USERTYPE:
        	 DoctorPanel.createWindow(name, myDataManager); //Creates the DoctorPanel
        	 Lframe.dispose();
        	 break;
         case GlobalVars.ADMIN_USERTYPE:
        	 SystemAdminPanel.createWindow(name, myDataManager); //Creates the Admin Panel 	 
        	 Lframe.dispose();
        	 break;
        default:
        	break;

	}
   }

	/**
	 * 
	 * @return Lframe
	 */
	public static JFrame createWindow() {
		Lframe.setVisible(true);
		return Lframe;
		}
	
}

